﻿using System;
using System.Collections;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.HCLoad.Parameters;
using YArchitech.HCLoad.Utils;
using YArchitech.HCLoad.XmlData;
using YArchitech.Revit;

using Autodesk.Revit.DB.Analysis;

namespace YArchitech.HCLoad.DataBinding
{
	public class SpaceBinding
	{
		private static bool IsHcWhTempl(WhTempl whTempl)
		{
			return whTempl != null && whTempl.SufferId > 0;
		}

		public static bool CheckConstruction(Construction rvtConstruction, List<WhTempl> listHcWhTempls)
		{
			if (rvtConstruction == null && (listHcWhTempls == null || listHcWhTempls.Count == 0))
			{
				return true;
			}
			string name = rvtConstruction.Name;
			foreach (WhTempl whTempl in listHcWhTempls)
			{
				string name2 = whTempl.Name;
				if (name.CompareTo(name2) != 0)
				{
					return false;
				}
			}
			return false;
		}

		public static bool CheckConstructions(MEPBuildingConstruction mepConstructions, Dictionary<HcObjectType, List<WhTempl>> HcConstructions)
		{
			Construction buildingConstruction = mepConstructions.GetBuildingConstruction((ConstructionType)0);
			List<WhTempl> listHcWhTempls = HcConstructions[HcObjectType.LO_OutWall];
			bool flag = SpaceBinding.CheckConstruction(buildingConstruction, listHcWhTempls);
			Construction buildingConstruction2 = mepConstructions.GetBuildingConstruction((ConstructionType)1);
			List<WhTempl> listHcWhTempls2 = HcConstructions[HcObjectType.LO_InWall];
			bool flag2 = SpaceBinding.CheckConstruction(buildingConstruction2, listHcWhTempls2);
			Construction buildingConstruction3 = mepConstructions.GetBuildingConstruction((ConstructionType)3);
			List<WhTempl> listHcWhTempls3 = HcConstructions[HcObjectType.LO_Roof];
			bool flag3 = SpaceBinding.CheckConstruction(buildingConstruction3, listHcWhTempls3);
			Construction buildingConstruction4 = mepConstructions.GetBuildingConstruction((ConstructionType)5);
			List<WhTempl> listHcWhTempls4 = HcConstructions[HcObjectType.LO_Stringboard];
			bool flag4 = SpaceBinding.CheckConstruction(buildingConstruction4, listHcWhTempls4);
			mepConstructions.GetBuildingConstruction((ConstructionType)6);
			List<WhTempl> list = HcConstructions[HcObjectType.LO_OutDoor];
			List<WhTempl> list2 = HcConstructions[HcObjectType.LO_InDoor];
			Construction buildingConstruction5 = mepConstructions.GetBuildingConstruction((ConstructionType)7);
			List<WhTempl> listHcWhTempls5 = HcConstructions[HcObjectType.LO_OutWindow];
			bool flag5 = SpaceBinding.CheckConstruction(buildingConstruction5, listHcWhTempls5);
			Construction buildingConstruction6 = mepConstructions.GetBuildingConstruction((ConstructionType)8);
			List<WhTempl> listHcWhTempls6 = HcConstructions[HcObjectType.LO_InWindow];
			bool flag6 = SpaceBinding.CheckConstruction(buildingConstruction6, listHcWhTempls6);
			Construction buildingConstruction7 = mepConstructions.GetBuildingConstruction((ConstructionType)9);
			List<WhTempl> listHcWhTempls7 = HcConstructions[HcObjectType.LO_SkyLight];
			bool flag7 = SpaceBinding.CheckConstruction(buildingConstruction7, listHcWhTempls7);
			return flag && flag2 && flag3 && flag4 && flag5 && flag6 && flag7;
		}

		private static bool IsConstructionChanged(MEPBuildingConstruction mepConstructions, Dictionary<HcObjectType, List<WhTempl>> HcConstructions, ref Dictionary<HcObjectType, List<WhTempl>> dicConstructionDifs)
		{
			bool result = false;
			foreach (KeyValuePair<HcObjectType, List<WhTempl>> keyValuePair in HcConstructions)
			{
				HcObjectType key = keyValuePair.Key;
				Dictionary<HcObjectType, List<WhTempl>>.Enumerator enumerator;
                //keyValuePair = enumerator.Current;
				foreach (WhTempl whTempl in keyValuePair.Value)
				{
					if (SpaceBinding.IsHcWhTempl(whTempl))
					{
						Room.AddToDictionary(key, whTempl, dicConstructionDifs);
						result = true;
					}
				}
			}
			return result;
		}

		public static bool CheckChange(Space space, Room room)
		{
			if (space.SpaceConstruction.CurrentConstruction == null)
			{
				return false;
			}
            HcUtils.IsZero(space.Area - HcUnit.CovertToAPI(room.RoomArea, (DisplayUnitType)12));
			bool result = false;
			if (room.RoomLoadCollect == null)
			{
				return true;
			}
			IDictionaryEnumerator enumerator = room.RoomLoadCollect.GetEnumerator();
			enumerator.Reset();
			while (enumerator.MoveNext())
			{
				RoomLoad roomLoad = enumerator.Value as RoomLoad;
				if (roomLoad != null && roomLoad.WhTempl != null && roomLoad.WhTempl.SufferId > 0)
				{
					result = true;
					DatabaseBinding.DataChanged = true;
					DatabaseBinding.AppendChange(space, roomLoad);
				}
			}
			return result;
		}

		public static bool Binding(UIApplication rvtUIAPP, Room room, SpaceAttributeFile spAttFile)
		{
			if (room == null)
			{
				return false;
			}
			int num = 0;
			if (!int.TryParse(room.GuidString, out num))
			{
				return false;
			}
			ElementId elementId = new ElementId(num);
			Space space = rvtUIAPP.ActiveUIDocument.Document.GetElementById(elementId) as Space;
			if (space == null)
			{
				return false;
			}
			SpaceBinding.CheckChange(space, room);
			RoomResult roomResult = new RoomResult(room);
			SpaceBinding.CopyResult(space, roomResult, spAttFile);
			return true;
		}

		private static bool CopyResult(Space space, RoomResult roomResult, SpaceAttributeFile apAttFile)
		{
			if (space == null || roomResult == null)
			{
				return false;
			}
			int maxValueTime = roomResult.MaxValueTime;
			foreach (SpaceAttributeGroup spaceAttributeGroup in apAttFile.Groups)
			{
				foreach (SpaceAttribute spaceAttribute in spaceAttributeGroup.Attributes)
				{
					Guid guid = new Guid(spaceAttribute.GUID);
					Parameter parameter = space.get_Parameter(guid);
					if (parameter != null)
					{
						HcResult hcResult = (HcResult)Enum.ToObject(typeof(HcResult), spaceAttribute.PropId);
						if (hcResult != HcResult.PeakCoolingLoadHour)
						{
							if (hcResult != HcResult.PeakIndoorCoolingLoadHour)
							{
								switch (hcResult)
								{
								case HcResult.FanCoilId:
								{
									long nID = roomResult.Room.FanCoil.m_nID;
									parameter.Set((double)nID);
									break;
								}
								case HcResult.FanCoilFactory:
								{
									string strFactory = roomResult.Room.FanCoil.m_strFactory;
									parameter.Set(strFactory);
									break;
								}
								case HcResult.FanCoilStruct:
								{
									string strStruct = roomResult.Room.FanCoil.m_strStruct;
									parameter.Set(strStruct);
									break;
								}
								case HcResult.FanCoilInstall:
								{
									string strInstall = roomResult.Room.FanCoil.m_strInstall;
									parameter.Set(strInstall);
									break;
								}
								case HcResult.FanCoilModel:
								{
									string strModel = roomResult.Room.FanCoil.m_strModel;
									parameter.Set(strModel);
									break;
								}
								case HcResult.FanCoilCount:
								{
									int nCount = roomResult.Room.FanCoil.m_nCount;
									parameter.Set(nCount);
									break;
								}
								case HcResult.AirVentStruct:
								{
									string strStruct2 = roomResult.Room.AirVent.m_strStruct;
									parameter.Set(strStruct2);
									break;
								}
								case HcResult.AirVentModel:
								{
									string strModel2 = roomResult.Room.AirVent.m_strModel;
									parameter.Set(strModel2);
									break;
								}
								case HcResult.AirVentLength:
								{
									double dLength = roomResult.Room.AirVent.m_dLength;
									parameter.Set(dLength);
									break;
								}
								case HcResult.AirVentWidth:
								{
									double dWidth = roomResult.Room.AirVent.m_dWidth;
									parameter.Set(dWidth);
									break;
								}
								case HcResult.AirVentHeight:
								{
									double dHeight = roomResult.Room.AirVent.m_dHeight;
									parameter.Set(dHeight);
									break;
								}
								case HcResult.AirVentDia:
								{
									double dD = roomResult.Room.AirVent.m_dD;
									parameter.Set(dD);
									break;
								}
								case HcResult.AirVentWindFlow:
								{
									double dWindFlow = roomResult.Room.AirVent.m_dWindFlow;
									parameter.Set(dWindFlow);
									break;
								}
								case HcResult.AirVentResistance:
								{
									double dPressure = roomResult.Room.AirVent.m_dPressure;
									parameter.Set(dPressure);
									break;
								}
								case HcResult.AirVentCount:
								{
									int nCount2 = roomResult.Room.AirVent.m_nCount;
									parameter.Set(nCount2);
									break;
								}
								default:
									parameter.Set(SpaceBinding.GetHCRoomResult(roomResult, spaceAttribute, maxValueTime));
									break;
								}
							}
							else
							{
								int peakIndoorCoolingLoadHour = roomResult.PeakIndoorCoolingLoadHour;
								parameter.Set(peakIndoorCoolingLoadHour);
							}
						}
						else
						{
							int peakCoolingLoadHour = roomResult.PeakCoolingLoadHour;
							parameter.Set(peakCoolingLoadHour);
						}
					}
				}
			}
			return true;
		}

		public static double ToStdValue(double dValue, SpaceAttribute spAtt)
		{
			double result = dValue;
			if (spAtt.DataType.CompareTo("HVAC_HEATING_LOAD") == 0 || spAtt.DataType.CompareTo("HVAC_COOLING_LOAD") == 0)
			{
                result = HcUnit.CovertToAPI(dValue, (DisplayUnitType)39);
			}
			else if (spAtt.DataType.CompareTo("HVAC_HEATING_LOAD_DIVIDED_BY_AREA") == 0 || spAtt.DataType.CompareTo("HVAC_COOLING_LOAD_DIVIDED_BY_AREA") == 0)
			{
                result = HcUnit.CovertToAPI(dValue, (DisplayUnitType)46);
			}
			return result;
		}

		private static double GetHCRoomResult(RoomResult roomResult, SpaceAttribute spAtt, int nMaxValueTime)
		{
			if (roomResult == null || spAtt == null)
			{
				return 0.0;
			}
			double dValue = 0.0;
			HcResult hcResult = (HcResult)Enum.ToObject(typeof(HcResult), spAtt.PropId);
			if (hcResult <= HcResult.CoolingRelativeHumidity)
			{
				if (hcResult <= HcResult.CoolingLoadofWall)
				{
					switch (hcResult)
					{
					case HcResult.CoolingLoad:
						dValue = roomResult.CoolingLoad[nMaxValueTime];
						break;
					case HcResult.CoolingSensibleLoad:
						dValue = roomResult.CoolingSensibleLoad[nMaxValueTime];
						break;
					case HcResult.CoolingLatentLoad:
						dValue = roomResult.CoolingLatentLoad[nMaxValueTime];
						break;
					case HcResult.IndoorCoolingLoad:
						dValue = roomResult.IndoorCoolingLoad[nMaxValueTime];
						break;
					case HcResult.IndoorCoolingSensibleLoad:
						dValue = roomResult.IndoorCoolingSensibleLoad[nMaxValueTime];
						break;
					case HcResult.IndoorCoolingLatentLoad:
						dValue = roomResult.IndoorCoolingLatentLoad[nMaxValueTime];
						break;
					case HcResult.CoolingHumidityLoad:
						dValue = roomResult.CoolingHumidityLoad[nMaxValueTime];
						break;
					case HcResult.IndoorCoolingHumidityLoad:
						dValue = roomResult.IndoorCoolingHumidityLoad[nMaxValueTime];
						break;
					case (HcResult)1011:
						break;
					case HcResult.CoolingVentilationLoad:
						dValue = roomResult.CoolingVentilationLoad[nMaxValueTime];
						break;
					case HcResult.CoolingLoadofSpacebyVentilation:
						dValue = roomResult.CoolingLoadofSpacebyVentilation[nMaxValueTime];
						break;
					case HcResult.CoolingSensibleLoadofSpacebyVentilation:
						dValue = roomResult.CoolingSensibleLoadofSpacebyVentilation[nMaxValueTime];
						break;
					case HcResult.CoolingLatentLoadofSpacebyVentilation:
						dValue = roomResult.CoolingLatentLoadofSpacebyVentilation[nMaxValueTime];
						break;
					case HcResult.CoolingHumidityLoadofSpacebyVentilation:
						dValue = roomResult.CoolingHumidityLoadofSpacebyVentilation[nMaxValueTime];
						break;
					case HcResult.CoolingLoadofSurfaces:
						dValue = roomResult.CoolingLoadofSurfaces[nMaxValueTime];
						break;
					default:
						switch (hcResult)
						{
						case HcResult.CoolingVentilationSensibleLoad:
							dValue = roomResult.CoolingVentilationSensibleLoad[nMaxValueTime];
							break;
						case HcResult.CoolingVentilationLatentLoad:
							dValue = roomResult.CoolingVentilationLatentLoad[nMaxValueTime];
							break;
						case HcResult.CoolingVentilationHumidityLoad:
							dValue = roomResult.CoolingVentilationHumidityLoad[nMaxValueTime];
							break;
						case HcResult.CoolingLoadofWall:
							dValue = roomResult.CoolingLoadofWall[nMaxValueTime];
							break;
						}
						break;
					}
				}
				else if (hcResult != HcResult.CoolingLoadofInteriorSurfaces)
				{
					if (hcResult != HcResult.CoolingLoadofExteriorSurfaces)
					{
						switch (hcResult)
						{
						case HcResult.CoolingLoadDensity:
							dValue = roomResult.CoolingLoadDensity[nMaxValueTime];
							break;
						case HcResult.HumidityLoadDensity:
							dValue = roomResult.HumidityLoadDensity[nMaxValueTime];
							break;
						case HcResult.IndoorCoolingLoadDensity:
							dValue = roomResult.IndoorCoolingLoadDensity[nMaxValueTime];
							break;
						case HcResult.IndoorHumidityLoadDensity:
							dValue = roomResult.IndoorHumidityLoadDensity[nMaxValueTime];
							break;
						case HcResult.DesignCoolT:
                            dValue = HcUnit.CovertToAPI(roomResult.DesignCoolT, (DisplayUnitType)57);
							break;
						case HcResult.CoolingRelativeHumidity:
							dValue = roomResult.CoolingRelativeHumidity;
							break;
						}
					}
					else
					{
						dValue = roomResult.CoolingLoadofExteriorSurfaces[nMaxValueTime];
					}
				}
				else
				{
					dValue = roomResult.CoolingLoadofInteriorSurfaces[nMaxValueTime];
				}
			}
			else if (hcResult <= HcResult.HeatingRelativeHumidity)
			{
				switch (hcResult)
				{
				case HcResult.HeatingLoad:
					dValue = roomResult.HeatingLoad;
					break;
				case HcResult.HeatingSensibleLoad:
					dValue = roomResult.HeatingSensibleLoad;
					break;
				case HcResult.HeatingLatentLoad:
					dValue = roomResult.HeatingLatentLoad;
					break;
				case HcResult.HeatingAirConditioningLoad:
					dValue = roomResult.HeatingAirConditioningLoad;
					break;
				case HcResult.HeatingAirConditioningSensibleLoad:
					dValue = roomResult.HeatingAirConditioningSensibleLoad;
					break;
				case HcResult.HeatingAirConditioningLatentLoad:
					dValue = roomResult.HeatingAirConditioningLatentLoad;
					break;
				case HcResult.HeatingAirConditioningHumidityLoad:
					dValue = roomResult.HeatingAirConditioningHumidityLoad;
					break;
				case HcResult.IndoorHeatingLoad:
					dValue = roomResult.IndoorHeatingLoad;
					break;
				case HcResult.IndoorHeatingSensibleLoad:
					dValue = roomResult.IndoorHeatingSensibleLoad;
					break;
				case HcResult.IndoorHeatingLatentLoad:
					dValue = roomResult.IndoorHeatingLatentLoad;
					break;
				case HcResult.HeatingHumidityLoad:
					dValue = roomResult.HeatingHumidityLoad;
					break;
				case HcResult.IndoorHeatingHumidityLoad:
					dValue = roomResult.IndoorHeatingHumidityLoad;
					break;
				case (HcResult)1513:
					break;
				case HcResult.HeatingVentilationLoad:
					dValue = roomResult.HeatingVentilationLoad;
					break;
				case HcResult.HeatingVentilationSensibleLoad:
					dValue = roomResult.HeatingVentilationSensibleLoad;
					break;
				case HcResult.HeatingVentilationLatentLoad:
					dValue = roomResult.HeatingVentilationLatentLoad;
					break;
				case HcResult.HeatingVentilationHumidityLoad:
					dValue = roomResult.HeatingVentilationHumidityLoad;
					break;
				default:
					switch (hcResult)
					{
					case HcResult.HeatingTotalLoadDensity:
						dValue = roomResult.HeatingTotalLoadDensity;
						break;
					case HcResult.HeatingHumidityLoadDensity:
						dValue = roomResult.HeatingHumidityLoadDensity;
						break;
					case HcResult.IndoorHeatingAirConditioningLoadDentisy:
						dValue = roomResult.IndoorHeatingAirConditioningLoadDentisy;
						break;
					case HcResult.HeatingAirConditioningLoadDensity:
						dValue = roomResult.HeatingAirConditioningLoadDensity;
						break;
					case HcResult.DesignHeatT:
                        dValue = HcUnit.CovertToAPI(roomResult.DesignHeatT, (DisplayUnitType)57);
						break;
					case HcResult.HeatingRelativeHumidity:
						dValue = roomResult.HeatingRelativeHumidity;
						break;
					}
					break;
				}
			}
			else
			{
				switch (hcResult)
				{
				case HcResult.BasicHeatingLoadofSurfaces:
					dValue = roomResult.BasicHeatingLoadofSurfaces;
					break;
				case HcResult.AdditionalHeatingLoadofSurfaces:
					dValue = roomResult.AdditionalHeatingLoadofSurfaces;
					break;
				case HcResult.GapInfiltrationHeatingLoad:
					dValue = roomResult.GapInfiltrationHeatingLoad;
					break;
				case HcResult.AirInfiltrationHeatingLoad:
					dValue = roomResult.AirInfiltrationHeatingLoad;
					break;
				case HcResult.HeatingLoadbyOpeningtheoutdoor:
					dValue = roomResult.HeatingLoadbyOpeningtheoutdoor;
					break;
				case HcResult.HeatingRadiatorTransferLoad:
					dValue = roomResult.HeatingRadiatorTransferLoad;
					break;
				case HcResult.HeatingRadiatorLoad:
					dValue = roomResult.HeatingRadiatorLoad;
					break;
				case HcResult.HeatingRadiatorbutTransferLoad:
					dValue = roomResult.HeatingRadiatorbutTransferLoad;
					break;
				default:
					if (hcResult != HcResult.HeatingRadiatorLoadDensity)
					{
						if (hcResult == HcResult.HeatingRadiatorbutTransferLoadDensity)
						{
							dValue = roomResult.HeatingRadiatorbutTransferLoadDensity;
						}
					}
					else
					{
						dValue = roomResult.HeatingRadiatorLoadDensity;
					}
					break;
				}
			}
			return SpaceBinding.ToStdValue(dValue, spAtt);
		}
	}
}
